import 'package:flutter/material.dart';

class SampleStickyHeader extends StatefulWidget {
  const SampleStickyHeader({Key? key}) : super(key: key);

  @override
  _SampleStickyHeaderState createState() => _SampleStickyHeaderState();
}

class _SampleStickyHeaderState extends State<SampleStickyHeader>
    with SingleTickerProviderStateMixin {
  late TabController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _controller = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          floating: true,
          snap: true,
          pinned: true,
          expandedHeight: 250,
          flexibleSpace: FlexibleSpaceBar(
            title: Text(
              "SampleSliverAppbar",
              textAlign: TextAlign.center,
            ),
            background: Image.network(
              'https://img2.baidu.com/it/u=2184053653,3921542411&fm=26&fmt=auto',
              fit: BoxFit.cover,
            ),
          ),
        ),
        SliverPersistentHeader(
            pinned: true,
            delegate: StickyTabBarDelegate(
                child: TabBar(
              controller: _controller,
              labelColor: Colors.black54,
              tabs: [
                Tab(text: 'Home'),
                Tab(text: 'Profile'),
              ],
            ))),
        SliverFillRemaining(
          child: TabBarView(
            controller: _controller,
            children: [
              Center(child: Text('Content of Home')),
              Center(child: Text('Content of Profile')),
            ],
          ),
        )
      ],
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;

  StickyTabBarDelegate({required this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return this.child;
  }

  @override
  double get maxExtent => this.child.preferredSize.height;

  @override
  double get minExtent => this.child.preferredSize.height;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) =>
      true;
}
